﻿using MyFunction;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Util.CellWalk;
using NPOI.XSSF.Streaming;

namespace CalendarToExcel
{
    internal class Program
    {
        const int width = 8;
        const int height = 10;
        static void Main(string[] args)
        {
            var obj = new MyMath();
            int answer = obj.MyAdd(width, height);
            //创建工作簿
            var wk = new SXSSFWorkbook();
            //创建工作表
            var st = wk.CreateSheet("输出日历");
            //定义行，列定位数值
            int x = 0, y = 0;
            //定义Excel行列
            var row = st.CreateRow(0);
            var cell = row.CreateCell(0);
            //创建行
            for (int i = 0; i < height * 10; i++)
                row = st.CreateRow(i);
            //进行循环，直到年数变成下一年
            //定义循环天数为days
            for (DateTime days = new DateTime(2022, 1, 1); days.Year == 2022; days = days.AddDays(1))
            {
                //计算这一天是周几
                int DoW = (int)days.DayOfWeek;
                //x0作为用来++的变量
                int x0 = (days.Month - 1) % 3 * width + DoW;
                //对每月的第一天
                if (days.Day == 1)
                {
                    x = (days.Month - 1) % 3 * width;
                    y = ((days.Month - 1) / 3) * height;
                    
                    //在此处不能创建行
                    //要用GetRow函数得到行，在该行创建cell（也不能用GetCell）
                    row = st.GetRow(y);
                    //输出当月月份
                    row.CreateCell(x).SetCellValue($"{days.Month}月");
                    //在下一行输出星期定位
                    row = st.GetRow(++y);
                    row.CreateCell(x).SetCellValue("日");
                    row.CreateCell(++x).SetCellValue("一");
                    row.CreateCell(++x).SetCellValue("二");
                    row.CreateCell(++x).SetCellValue("三");
                    row.CreateCell(++x).SetCellValue("四");
                    row.CreateCell(++x).SetCellValue("五");
                    row.CreateCell(++x).SetCellValue("六");
                    //在下一行开始输出日期
                    row = st.GetRow(++y);
                    cell = row.CreateCell(x0);
                }
                cell.SetCellValue($"{days.ToString("dd")}  ");
                if (DoW == 6)
                {
                    row = st.GetRow(++y);
                    cell = row.CreateCell((days.Month - 1) % 3 * width);
                }
                else
                    cell = row.CreateCell(++x0);
           

            }


            var fs = new FileStream(@"C:\Users\Ma Chenhe\Desktop\CalendarToExcel.xlsx", FileMode.Create, FileAccess.Write);
            wk.Write(fs);
            fs.Close();
            Console.WriteLine("Finished!");

        }
    }
}